#include "message-client-client.h"
#include "log.h"

namespace messages {

// HELLO
REGISTER_MESSAGE_TYPE(HELLO, Hello);

Hello::Hello() : Message(HELLO) {
}

Hello::Hello(ClientId id) :
    Message(HELLO), id_(id) {
}

void Hello::build_array(QByteArray* array) const {
    Message::build_array(array);

    ByteArrayAppender a(array);
    a.append(id_);
}

void Hello::parse_array(const QByteArray* array, int* position) {
    Message::parse_array(array, position);

    ByteArrayParser p(array, position);
    p.parse(&id_);
}

// ASK_ABOUT_FILE
REGISTER_MESSAGE_TYPE(ASK_ABOUT_FILE, AskAboutFile);

AskAboutFile::AskAboutFile() : Message(ASK_ABOUT_FILE) {
}

AskAboutFile::AskAboutFile(const FingerPrint& finger) :
    Message(ASK_ABOUT_FILE), finger_(finger) {
}

void AskAboutFile::build_array(QByteArray* array) const {
    Message::build_array(array);

    ByteArrayAppender a(array);
    a.append(finger_);
}

void AskAboutFile::parse_array(const QByteArray* array, int* position) {
    Message::parse_array(array, position);

    ByteArrayParser p(array, position);
    p.parse(&finger_);
}

// S_AVAILABLE_CHUNKS
REGISTER_MESSAGE_TYPE(SEND_AVAILABLE_CHUNKS, SendAvailableChunks);

SendAvailableChunks::SendAvailableChunks() : Message(SEND_AVAILABLE_CHUNKS) {
}

SendAvailableChunks::SendAvailableChunks(const QByteArray& bitmap) :
    Message(SEND_AVAILABLE_CHUNKS), bitmap_(bitmap) {
}

void SendAvailableChunks::build_array(QByteArray* array) const {
    Message::build_array(array);

    ByteArrayAppender a(array);
    a.append(bitmap_);
}

void SendAvailableChunks::parse_array(const QByteArray* array, int* position) {
    Message::parse_array(array, position);

    ByteArrayParser p(array, position);
    p.parse(&bitmap_);
}


// SEND_FILE_PART
REGISTER_MESSAGE_TYPE(SEND_FILE_PART, SendFilePart);

SendFilePart::SendFilePart() : Message(SEND_FILE_PART) {
}

SendFilePart::SendFilePart(const FingerPrint& finger, const QByteArray& data) :
    Message(SEND_FILE_PART), finger_(finger),data_(data) {
}

void SendFilePart::build_array(QByteArray* array) const {
    Message::build_array(array);

    ByteArrayAppender a(array);
    a.append(finger_);
    a.append(data_);
}

void SendFilePart::parse_array(const QByteArray* array, int* position) {
    Message::parse_array(array, position);

    ByteArrayParser p(array, position);
    p.parse(&finger_);
    p.parse(&data_);
}

// ASK_FILE_PART
REGISTER_MESSAGE_TYPE(ASK_FILE_PART, AskFilePart);

AskFilePart::AskFilePart() : Message(ASK_FILE_PART) {
}

AskFilePart::AskFilePart(const FingerPrint& finger, quint32 start, quint32 length) :
    Message(ASK_FILE_PART), finger_(finger),start_(start),length_(length) {
}

void AskFilePart::build_array(QByteArray* array) const{
    Message::build_array(array);

    ByteArrayAppender a(array);
    a.append(finger_);
    a.append(start_);
    a.append(length_);
}

void AskFilePart::parse_array(const QByteArray* array, int* position) {
    Message::parse_array(array, position);

    ByteArrayParser p(array, position);
    p.parse(&finger_);
    p.parse(&start_);
    p.parse(&length_);
}

}

